package com.ruoyi.tcsp.service.impl;

import com.ruoyi.tcsp.domain.bean.req.TcspSciEvalReq;
import com.ruoyi.tcsp.domain.bean.resp.SciEvalResp;
import com.ruoyi.tcsp.domain.mysql.ScientificBaseInfo;
import com.ruoyi.tcsp.domain.mysql.TcspSciEval;
import com.ruoyi.tcsp.mapper.ScientificBaseInfoMapper;
import com.ruoyi.tcsp.mapper.TcspSciEvalMapper;
import com.ruoyi.tcsp.service.ITcspSciEvalService;
import com.ruoyi.tcsp.utils.Constants;
import com.ruoyi.tcsp.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ITcspSciEvalServiceImpl implements ITcspSciEvalService {
    @Autowired
    private ScientificBaseInfoMapper scientificBaseInfoMapper;
    @Autowired
    private TcspSciEvalMapper tcspSciEvalMapper;

    @Override
    @Transactional
    public int scienEval(TcspSciEvalReq req, String username) {
        ScientificBaseInfo updateInfo = new ScientificBaseInfo();
        updateInfo.setScientificId(req.getScienId());
        updateInfo.setStatus(req.getEvalStatus());
        updateInfo.setEvaluateLeval(req.getEvalLevel());
        //TODO 通过，科技需要设置公开日和公开号
        if (req.getEvalStatus().equals(Constants.THE_NEW_SIC_STATUS_ACCEPT)) {
            updateInfo.setPublicDate(new Date());
            updateInfo.setPublicNumber(Utils.getPublicNum());
        }
        //更新科技成果表
        int res1= scientificBaseInfoMapper.updateScientificBaseInfo(updateInfo);
        //插入成果评估表
        TcspSciEval tcspSciEval = new TcspSciEval();
        tcspSciEval.setScienId(req.getScienId());
        tcspSciEval.setEvalRemark(req.getEvalRemark());
        tcspSciEval.setEvalStatus(req.getEvalStatus());
        tcspSciEval.setEvalTime(new Date());
        tcspSciEval.setEvalUser(username);
        tcspSciEval.setEvalLevel(req.getEvalLevel());
        int res2 = tcspSciEvalMapper.insertTcspSciEval(tcspSciEval);
        return res1 + res2;
    }

    @Override
    public List<SciEvalResp> getScienEval(String scienId) {
        List<SciEvalResp> list = tcspSciEvalMapper.selectTcspSciEvalByScienId(scienId);
        return list;
    }
}
